package io.renren.wechat.support;

import org.apache.commons.lang3.ArrayUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

/**
 * @author dmcp@qq.com
 * @since 2021/11/29 22:37
 */
public abstract class CorpUtils {
    private static final Logger LOGGER = LoggerFactory.getLogger(CorpUtils.class);


    public static String getCorpIdFromRequestCookie(HttpServletRequest request) {
        final Cookie[] cookies = request.getCookies();
        if (ArrayUtils.isEmpty(cookies)) {
            return null;
        }

        for (Cookie cookie : cookies) {
            if ("corpId".equals(cookie.getName())) {
                LOGGER.debug("get corpId from request cookie successfully, cookie: {}", cookie);
                return cookie.getValue();
            }
        }

        return null;
    }

    public static String getCorpIdFromRequestHeader(HttpServletRequest request) {
        return request.getHeader("corpId");
    }
}
